package com.example.demo.util;

import org.springframework.stereotype.Component;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

@Component
public class TransactionUtil {

    private final PlatformTransactionManager transactionManager;

    public TransactionUtil(PlatformTransactionManager transactionManager) {
        this.transactionManager = transactionManager;
    }

    public TransactionStatus beginTransaction() {
        TransactionDefinition transactionDefinition = new DefaultTransactionDefinition();
        return transactionManager.getTransaction(transactionDefinition);
    }

    public void commitTransaction(TransactionStatus transactionStatus) {
        transactionManager.commit(transactionStatus);
    }

    public void rollbackTransaction(TransactionStatus transactionStatus) {
        transactionManager.rollback(transactionStatus);
    }
}

